/**
 * Created by Administrator on 2016/12/23.
 */

var arrAutoClosePopup = [];
var flagOpenedByPageLoad = !1;
var timeId = null;
var flag_dragged = !1,board_id = 0, MAX_LANE_COUNT = 500, MAX_TASK_COUNT = 1E3;

function getTaskIdFromMySelfId(a) {
    return $(a)[0].id.substr(5)
}
function addLaneByNormalClick() {
    autoCloseOtherPopup();
    $("#div_list_add").removeAttr("data-lane-index");
    addLaneWhenSubmit()
}
function addLaneWhenSubmit() {
    var a = $("#div_list_add").attr("data-lane-index"), c = "undefined" == typeof a;
    0 == $("#div_board .list-header-wrapper").find(".list-header").length ? ($("#result").removeClass("hidden"), $("#result").html("\u8bf7\u81f3\u5c11\u6dfb\u52a0\u4e00\u4e2a\u5217\u8868\u540e\u518d\u6dfb\u52a0\u6cf3\u9053")) : $(".div_lane").length >= MAX_LANE_COUNT ? (swal({
        title: "",
        text: "\u6cf3\u9053\u5df2\u8fbe\u5230\u4e0a\u9650",
        type: "warning",
        timer: 2E3,
        showConfirmButton: !1,
        allowOutsideClick: !0
    }), closeCurrentPopup()) :
        ($("#result").addClass("hidden"), c && (a = -1), $.post(root_url + "/lane/add", {
            board_id: board_id,
            div_lane_index: a
        }, function (b) {
            if (0 == b.succeed)"lane_overrun" == b.message && (swal({
                title: "",
                text: "\u6cf3\u9053\u5df2\u8fbe\u5230\u4e0a\u9650",
                type: "warning",
                timer: 2E3,
                showConfirmButton: !1,
                allowOutsideClick: !0
            }), closeCurrentPopup()); else if (1 == b.succeed) {
                var c = $("#div_board").children(".div_lane").length;
                add_lane(b.message.lane_id, b.message.allListIdBlockId, b.message.div_lane_index);
                $("#div_list_add").addClass("hidden");
                hideLanehandleBtn();
                a == c - 1 || -1 == a ? div_board.scrollTop(1E6) : div_board.scrollTop(div_board.scrollTop() + 59)
            }
        }, "json"))
}
function loadTaskTags(a) {
    a = $(a).find(".tag_on_card");
    if (!(0 >= a.length)) {
        var c = "";
        $.each(a, function (a, d) {
            var e = $(d).data("tag_code"), g = getColorCodeByTagCode(e), g = getTagColorByColorCode(g), f = getTagNameByTagCode(e);
            c += tmpl("template-tag-on-task-edit", {tag_code: e, tag_color: g, tag_name: f})
        });
        $("#btn-add-tag").prevAll().remove();
        $(c).insertBefore($("#btn-add-tag"));
        $(".taskTags").removeClass("hidden")
    }
}

function getTagColorByColorCode(a) {
    var c = "";
    $(tagColor).each(function (b) {
        this.tag_code == a && (c = this.tag_color)
    });
    return c
}
function getTagNameByTagCode(a) {
    var c = "";
    $(g_board.tags).each(function () {
        this.tag_code == a && (c = this.tag_name)
    });
    return c
}
function getColorCodeByTagCode(a) {
    var c = "";
    $(g_board.tags).each(function () {
        this.tag_code == a && (c = this.color_code)
    });
    return c
}

function showEditTaskDiv(a, c) {
    var b = $("#divTaskEditWapper");
    $("#divTaskChecklistTitle,#divAddChklstItem,#divTaskAttachmentTitle,.taskTags").addClass("hidden");
    $("#edit_task_name").removeClass("gray-background");
    $("#divEditTaskName .btn-div").children().hide();
    $("#divTaskContentInput .btn-div").children().hide();
    $("#taskDescriptionDiv .btn-div").children().hide();
    $("#divTaskContentInput .btn-div").css("height", "0");
    $("#divEditTaskName .btn-div").css("height", "0");
    $("#taskDescriptionDiv .btn-div").css("height",
        "0");
    $("#divAddChklstItem textarea").val("");
    $("#divCardTimeLine .vertical-timeline").html("");
    $("#btn-add-tag").prevAll().remove();
    clearErrorWarning($(".task_comment_input_error"));
    clearErrorWarning($(".lbl_edit_task_name_msg"));
    resetMarkdownEditor();
    flagOpenedByPageLoad || autoCloseOtherPopup();
    clearTimeout(timeId);
    timeId = setTimeout(function () {
        b.show();
        target_divTaskView = (c = void 0 != c && 1 == c) ? a.closest(".task_view") : a;
        if (1 == flag_dragged)flag_dragged = !1; else {
            current_task_id = getTaskIdFromMySelfId(target_divTaskView);
            b.data("target_task_id", current_task_id);
            $("#divTaskEditWapper").modal({keyboard: !1});
            loadTaskTags(a);
            loadTaskAttachmentList();
            toggleChecklistDiv_inTaskEdit();
            loadTaskComment();
            resetAddCommentTxt();
            initMardownScript();
            useDesttopCSS() || ($("#divEditButtonGroup").insertAfter("#btnCloseIndivTaskEdit"), $("#divColorList_tag").css("top", "0"), $("#divChecklist").css("top", "93px"), toggleTaskUnlinkUserDiv.css("top", "93px"));
            $("#divAddOrSetTag").css("display", "none");
            $("#divDeleteTask").css("display", "none");
            $("#divWorkEstimate").css("display", "none");
            $("#divChecklist").css("display", "none");
            $("#divAddAttachment").css("display", "none");
            $("#divAddUser").css("display", "none");
            var d = $(target_divTaskView).data("task_name"), d = $.trim(d).replaceAll("</br>", "\n");
            task_name_before_edit = taskNameForCancle = d = htmlspecialchars_decode(d);
            $("#edit_task_name").css("height", "36px");
            $("#edit_task_name").val(d);
            d = document.getElementById("edit_task_name");
            autoTextarea(d);
            $("#divColorList_tag").find(".fa-check").each(function (a) {
                $(this).addClass("hidden")
            });
            d = $(target_divTaskView).data("tags");
            if (0 < $.trim(d).length)for (var d = d.toString().split(","), e = 0; e < d.length; e++) {
                var g = d[e];
                2 == g.length && $("#divColorList_tag").find("li.tag" + g).children(".fa-check").removeClass("hidden")
            }
            last_task_in_editor_div.task_id = current_task_id;
            b.children("div.modal-backdrop").unbind("click")
        }
    }, 200);
    arrAutoClosePopup = [];
    arrAutoClosePopup.unshift({popupDiv: b});
    flagOpenedByPageLoad || (getEvent().stopPropagation(), flagOpenedByPageLoad = !1);
    $(".boardSelectPopup").hide()
}

function showAddTaskDiv(a) {
    addTaskOnListTop = !1;
    autoCloseOtherPopup();
    current_list_id = getListId_fromBlock(a);
    current_block_id = getBlockId(a);
    $(a).parent().addClass("hidden");
    var c = $("#block_" + current_block_id), b = c.find(".div_add_task");
    c.find(".div_block_body").append(b);
    $(b).removeClass("hidden");
    $(b).css("padding", 0);
    var d = getBoardHeight() - 89;
    $(c.find(".div_block_body")).css("max-height", d);
    $(c.find(".div_block_body")).scrollTop(1E4);
    $(b).find('textarea[name="new_task_name"]').focus();
    useDesttopCSS() ?
        IsIE() ? (c = $(a).parent().parent().parent().height(), c < d ? $(a).parent().parent().removeClass("panel-footer lane-list-footer").addClass("ui-sortable-handle") : $(a).parent().parent().removeClass("panel-footer lane-list-footer").css("padding-bottom", 5).addClass("ui-sortable-handle")) : IsEdge() ? (c = $(a).parent().parent().parent().height(), c < d ? $(a).parent().parent().removeClass("panel-footer lane-list-footer").addClass("ui-sortable-handle") : $(a).parent().parent().removeClass("panel-footer").addClass("ui-sortable-handle")) :
            $(a).parent().parent().removeClass("panel-footer lane-list-footer").addClass("ui-sortable-handle") : $(a).parent().parent().find("br").remove();
    arrAutoClosePopup.unshift({popupDiv: $(b), functionWhenClosePopup: showAddTaskLink});
    getEvent().stopPropagation()
}

function getFocus(a) {
    "TEXTAREA" != document.activeElement.tagName && me.can_edit_board && a.focus()
}

function showAddListDivWithMouse(a) {
    a = a || window.event;
    if (2 == a.eventPhase && me.can_edit_board) {
        $("#result").addClass("hidden");
        $("#add_list_panel").addClass("hidden");
        $("#div_list_add .add-list-lane-btn").css("margin-bottom", "0");
        var c = getMouseCoordinate(a), b = $("#div_list_add"), d = c.x, e = c.y, c = div_board.scrollLeft(), g = div_board.scrollTop(), f = $(".list-header-wrapper .list-header").outerWidth() + 10, f = Math.round(parseInt((d + c) / (f / 2)) / 2), h = 0, k = $(".div_lane").length;
        $.each($(".div_lane:visible"), function (a,
                                                 b) {
            var c = $(b).offset().top, d = $(b).offset().top + $(b).height() + 31;
            if (e >= c && e <= d)return h = a, !1;
            a == k - 1 && (h = a)
        });
        $(window).width() - d < b.width() && (d = $(window).width() - b.width() - 15);
        135 > $(window).height() - e && (e = $(window).height() - 140);
        $(".add_list_label").removeClass("hidden");
        b.find(".form-group").eq(0).removeClass("hidden");
        b.removeClass("hidden").appendTo(a.target).css({position: "absolute", top: e - 100 + g, left: d + c});
        b.find("input").focus().val("");
        b.attr("data-list-index", f);
        b.attr("data-lane-index", h);
        arrAutoClosePopup.unshift({
            popupDiv: b,
            functionWhenClosePopup: showAddListLabel
        });
        a.stopPropagation()
    }
}

function showEditTaskDiv(a, c) {
    var b = $("#divTaskEditWapper");
    $("#divTaskChecklistTitle,#divAddChklstItem,#divTaskAttachmentTitle,.taskTags").addClass("hidden");
    $("#edit_task_name").removeClass("gray-background");
    $("#divEditTaskName .btn-div").children().hide();
    $("#divTaskContentInput .btn-div").children().hide();
    $("#taskDescriptionDiv .btn-div").children().hide();
    $("#divTaskContentInput .btn-div").css("height", "0");
    $("#divEditTaskName .btn-div").css("height", "0");
    $("#taskDescriptionDiv .btn-div").css("height",
        "0");
    $("#divAddChklstItem textarea").val("");
    $("#divCardTimeLine .vertical-timeline").html("");
    $("#btn-add-tag").prevAll().remove();
    clearErrorWarning($(".task_comment_input_error"));
    clearErrorWarning($(".lbl_edit_task_name_msg"));
    resetMarkdownEditor();
    flagOpenedByPageLoad || autoCloseOtherPopup();
    clearTimeout(timeId);
    timeId = setTimeout(function () {
        b.show();
        target_divTaskView = (c = void 0 != c && 1 == c) ? a.closest(".task_view") : a;
        if (1 == flag_dragged)flag_dragged = !1; else {
            current_task_id = getTaskIdFromMySelfId(target_divTaskView);
            b.data("target_task_id", current_task_id);
            $("#divTaskEditWapper").modal({keyboard: !1});
            loadTaskTags(a);
            loadTaskAttachmentList();
            toggleChecklistDiv_inTaskEdit();
            loadTaskComment();
            resetAddCommentTxt();
            initMardownScript();
            useDesttopCSS() || ($("#divEditButtonGroup").insertAfter("#btnCloseIndivTaskEdit"), $("#divColorList_tag").css("top", "0"), $("#divChecklist").css("top", "93px"), toggleTaskUnlinkUserDiv.css("top", "93px"));
            $("#divAddOrSetTag").css("display", "none");
            $("#divDeleteTask").css("display", "none");
            $("#divWorkEstimate").css("display", "none");
            $("#divChecklist").css("display", "none");
            $("#divAddAttachment").css("display", "none");
            $("#divAddUser").css("display", "none");
            var d = $(target_divTaskView).data("task_name"), d = $.trim(d).replaceAll("</br>", "\n");
            task_name_before_edit = taskNameForCancle = d = htmlspecialchars_decode(d);
            $("#edit_task_name").css("height", "36px");
            $("#edit_task_name").val(d);
            d = document.getElementById("edit_task_name");
            autoTextarea(d);
            $("#divColorList_tag").find(".fa-check").each(function (a) {
                $(this).addClass("hidden")
            });
            d = $(target_divTaskView).data("tags");
            if (0 < $.trim(d).length)for (var d = d.toString().split(","), e = 0; e < d.length; e++) {
                var g = d[e];
                2 == g.length && $("#divColorList_tag").find("li.tag" + g).children(".fa-check").removeClass("hidden")
            }
            last_task_in_editor_div.task_id = current_task_id;
            b.children("div.modal-backdrop").unbind("click")
        }
    }, 200);
    arrAutoClosePopup = [];
    arrAutoClosePopup.unshift({popupDiv: b});
    flagOpenedByPageLoad || (getEvent().stopPropagation(), flagOpenedByPageLoad = !1);
    $(".boardSelectPopup").hide()
}
function clearErrorWarning(a) {
    $(a).addClass("hidden");
    $(a).html("")
}
function resetMarkdownEditor() {
    $("#divTaskDescTitle").removeClass("animated fadeIn").addClass("hidden");
    $("#divTaskDescTitle span:last").text("");
    $("#taskDescriptionDiv").removeClass("animated fadeIn");
    var a = $(".lbl_edit_task_desc_msg"), c = $(".btn-save-task-desc");
    $("#task_desc_input").val("").removeAttr("rows style").addClass("hidden");
    $("#taskDescriptionDiv").html($("#task_desc_input"));
    a.insertAfter($("#task_desc_input"));
    c.insertAfter(a)
}
function showAddListDiv(a) {
    autoCloseOtherPopup();
    if (me.can_edit_board) {
        var c = $("#div_list_add");
        c.find("#result").addClass("hidden");
        $("#add_list_panel").removeClass("hidden");
        c.find(".add-list-lane-btn").removeClass("hidden");
        c.insertAfter(a);
        c.removeAttr("style");
        c.removeAttr("data-list-index");
        c.find(".form-group").eq(0).addClass("hidden");
        c.removeClass("hidden");
        c.find(':text[name="new_list_name"]').focus().val("");
        c.prev().addClass("hidden");
        arrAutoClosePopup.unshift({popupDiv: c, functionWhenClosePopup: showAddListLabel});
        getEvent().stopPropagation()
    }
}
function isShown(a) {
    var c = a;
    "string" === jQuery.type(a) && (c = $("#" + a));
    return 0 == c.length ? !1 : !c.hasClass("hidden") && "none" != c.css("display")
}

function showAddTaskLink(a) {
    var c = a.parent();
    "true" == is_mobile ? a.parents(".div_block_body_wrapper").next().removeClass("hidden") : (c.next().find(".hide-block-btn").removeClass("hidden"), IsIE() || IsEdge() ? (c.parent().find(".ui-sortable-handle").removeClass().addClass("panel-footer lane-list-footer ui-sortable-handle"), a.find(".form-group").next().next().css("padding-bottom", 0)) : c.parent().find(".ui-sortable-handle").addClass("panel-footer lane-list-footer"));
    a = getBoardHeight() - 89 - 33;
    c.css("max-height",
        a);
    c.scrollTop(1E4)
}

function addTask() {
    closeCurrentPopup();
    
    //var a = $("#block_" + current_block_id), c = $(a).find(".div_add_task"), b = $(c).find('textarea[name="new_task_name"]'), d = $(b).val(), e = root_url + "/task/add", g = getLaneId(a);
    //$(b).focus().val("");
    //if (0 == $.trim(d.replace(/[\r\n]/g, "")).length)return !1;
    //if (parseInt($("#list_" + current_list_id + " .task-amount").attr("data-amount")) >= MAX_TASK_COUNT)swal({
    //    title: "",
    //    text: "\u8be5\u5217\u8868\u5361\u7247\u6570\u5df2\u8fbe\u5230\u4e0a\u9650",
    //    type: "warning",
    //    timer: 2E3,
    //    showConfirmButton: !1,
    //    allowOutsideClick: !0
    //}),
    //    closeCurrentPopup(); else {
    //    var f = $(c).find(".label-warning");
    //    $(f).addClass("hidden");
    //    var h = createId(), k = "";
    //    addTaskOnListTop && 0 < c.next(".task_view").length && (k = c.next(".task_view")[0].id.substr(5));
    //    add_task({
    //        block_id: current_block_id,
    //        task_id: h,
    //        lane_id: g,
    //        task_name: htmlspecialchars(d, null, null),
    //        color_index: "",
    //        estimate: "",
    //        position_before_task: k,
    //        chklstitem_finished_amount: 0,
    //        chklstitem_total_amount: 0,
    //        has_comment: "N"
    //    });
    //    var m = $(div_board.find(".div_lane")[0]).find(".delete-lane"), q = div_board.find(".div_lane")[0].id;
    //    m.hasClass("hidden") && q == g && m.removeClass("hidden");
    //    m = $(a).find(".div_block_body");
    //    addTaskOnListTop || m.scrollTop(m[0].scrollHeight);
    //    setListDefaultMaxHeight();
    //    $(c).find("button")[0].focus();
    //    isIphoneWeixin() || $(b)[0].focus();
    //    c = {
    //        new_task_name: d,
    //        board_id: board_id,
    //        lane_id: g,
    //        list_id: current_list_id,
    //        block_id: current_block_id,
    //        task_id: h,
    //        list_name: getListName(current_list_id),
    //        position_before_task: k
    //    };
    //    $.post(e, c, function (a) {
    //        null != a && 0 == a.succeed && ("task_overrun" == a.message ? swal({
    //            title: "", text: "\u8be5\u5217\u8868\u5361\u7247\u6570\u5df2\u8fbe\u5230\u4e0a\u9650",
    //            type: "warning", timer: 2E3, showConfirmButton: !1, allowOutsideClick: !0
    //        }) : ($(f).parent().removeClass("hidden"), $(f).html(a.message)))
    //    }, "json");
    //    setTaskAmountOfList();
    //    if (IsIE() || IsEdge())e = $(a).css("max-height").split("px")[0], $(a).height() + 29 > e ? (m = $(a).find(".div_block_body"), $(a).children().find(".form-group").next().next().css("padding-bottom", 5)) : $(a).children().find(".form-group").next().next().css("padding-bottom", 0);
    //    1 != $(".task_view").length || useMobileCSS() || initSearchCard()
    //}
}

function getEvent() {
    var a;
    if (void 0 == window.event) {
        for (caller = arguments.callee; null != caller.caller;)caller = caller.caller;
        a = caller.arguments[0]
    } else a = window.event;
    return a
}

function closeCurrentPopup() {
    _closePopup();
    var a = getEvent();
    "undefined" != typeof a && 0 != a && a.stopPropagation()
}
function autoCloseOtherPopup() {
    var a = getEvent();
    if ("undefined" == typeof a.target || "undefined" == typeof a.target.tagName || "input" != a.target.tagName.toLowerCase() || "button" == a.target.type || "reset" == a.target.type)if (flag_lane_moved && (flag_lane_moved = !1, a = div_board.find(".shadow-focus"), $(a).find(".div_block").removeClass("lane-shadow"), $(a).removeClass("shadow-focus"), $(a).find(".lane-handle-btn").find("a").removeClass("lane-move-btn")), flag_shown_help && (clearPrompt(), flag_shown_help = !1), 0 != arrAutoClosePopup.length) {
        var c =
            arrAutoClosePopup[0].popupDiv, a = getEvent();
        0 == c.find(a.target).length ? isShown($("#ui-datepicker-div")) || _closePopup() : $(a.target).hasClass("modal-backdrop") && _closePopup()
    }
}

function getBlockId(a) {
    return $(a).closest(".div_block")[0].id.substr(6)
}
function getListId_fromBlock(a) {
    return $(a).closest(".div_block").data("list_id")
}
function getBoardHeight() {
    return $(window).height() - 90
}
function _closePopup() {
    if (0 != arrAutoClosePopup.length) {
        var a = arrAutoClosePopup[0].popupDiv;
        isShown(a) && (a.hasClass("modal") ? a.modal("hide") : "block" == a[0].style.display ? a.css("display", "none") : a.addClass("hidden"), void 0 != arrAutoClosePopup[0].functionWhenClosePopup && "function" == typeof arrAutoClosePopup[0].functionWhenClosePopup && arrAutoClosePopup[0].functionWhenClosePopup(a));
        arrAutoClosePopup.shift()
    }
}

function IsEdge() {
    return (match_str = navigator.userAgent.toLowerCase().match(/edge\/([\d.]+)/)) ? !0 : !1
}
function IsIE() {
    return (0 <= navigator.userAgent.indexOf("MSIE") || 0 <= navigator.userAgent.indexOf("Trident")) && 0 > navigator.userAgent.indexOf("Opera") ? !0 : !1
}
function isIPad() {
    return "ipad" == navigator.userAgent.toLowerCase().match(/iPad/i)
}

function useDesttopCSS() {
    return !useMobileCSS()
}
function useMobileCSS() {
    return "true" == is_mobile
}

function collapseViewIsOpen() {
    return isShown("navCollapseLaneBtnOn") ? !0 : !1
}
